package com.link.base.base.orgnization.service;

import com.link.base.base.orgnization.dao.mybatis.mapper.OrgImportMapper;
import com.link.base.base.orgnization.dao.mybatis.mapper.OrgnizationMapper;
import com.link.base.base.orgnization.model.Orgnization;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.model.CallParam;
import com.link.core.cllog.LCLogger;
import com.link.core.util.UserUtil;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.StringUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author unknown
 * @date unknown
 * @comments 组织导入服务
 */
@Service
public class OrgImportServiceImpl extends BasicServiceImpl<Orgnization> implements OrgImportService {

    @Resource
    private OrgImportMapper orgImportMapper;

    @Resource
    private OrgnizationMapper organizationMapper;

    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicMapper<Orgnization> getBasicMapper() {
        return orgImportMapper;
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void insert(Orgnization record) throws BasicServiceException {
        Long orgid = null;
        boolean flag = false;
        try {
            orgid = keyGenerateService.keyGenerate();
            record.setId(orgid);
            if (StringUtils.isNull(record.getParentOrgName())) {
                flag = true;
            }
            //如果父组织没有指定，则添加默认父组织
            if (flag) {
                //获取根组织
                Orgnization org = organizationMapper.queryRoot(record);
                record.setParentOrgName(org.getText());
            }
            orgImportMapper.insert(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new BasicServiceException(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void runOrganizationCheck(com.link.core.basic.model.CallParam record) throws BasicServiceException {
        try {
            CoreUser user = UserUtil.getUser();
            record.setParam1(user.getId());
            orgImportMapper.runOrganizationCheck(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new BasicServiceException(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void runNewOrganizationForimp(CallParam record) throws BasicServiceException {
        try {
            CoreUser user = UserUtil.getUser();
            record.setParam1(user.getId());
            orgImportMapper.runOrganizationCheck(record);
            orgImportMapper.runNewOrganizationForimp(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new BasicServiceException(e.getMessage());
        }
    }
}
